package com.example.book.controller;

import com.example.book.constant.Constants;
import com.example.book.model.Result;
import com.example.book.model.UserInfo;
import com.example.book.service.UserService;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpSession;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-14
 * Time: 21:35
 */

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public Result login(String userName , String password , HttpSession session){
        //1:校验参数
        if(!StringUtils.hasLength(userName)
                || !StringUtils.hasLength(password)){
            return Result.fail("用户名或者密码为空");
        }
//        //2：验证账户和密码
//        if(!"admin".equals(userName) || !"admin".equals(password)){
//            return "账户名错误或密码错误";
//        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo == null){
            return Result.fail("用户不存在");
        }
        if(!password.equals(userInfo.getPassword())){
            return Result.fail("密码错误");
        }
        session.setAttribute(Constants.USER_SESSION_KEY,userInfo);//建立会话
        //3：返回响应结果
        return Result.success("");
    }

}
